Let's work on the kills and deaths part of our leaderboard, something every first person shooter game
needs.
And I got the test server running with two players.
There's my other player.
I'm going to kill them.
Bam, We got a kit one for kills the other guy.
I got one for deaths and I awarded the player 50 points for the kill.
I thought that would be pretty useful.
Let's go ahead and get started.
We are going to use the creator tag pattern.
It's a popular roadblock programming technique.
Basically, when we damage a character, we place a tag on the humanoid with the attackers info so that
if the character dies soon after you hurt them, the last person to do damage gets credit for the kill.
You can find a lot of the same code in the roadblocks weapons.
Let's go to our pistol.
I have it in the starter pack simple pistol and then open up the damage script.
There we go.
All right.
Let's get down to lines and start defining our functions.
Do a local function tag humanoid.
I'm going to pass in the humanoid of the thing getting hurt and the player doing the hurting.
And then I'm going to make a variable for my creator.
Tag.
Create a tag equals instance.
New object value him.
So the parent is the humanoid that's getting hurt.
Let's go ahead and get our creator tag name and set that to creator.
You should keep this consistent because a lot of roadblocks, weapons already have this implemented
and they're calling it creator.
So you'll be able to use other roadblocks, weapons that have this seamlessly without doing anything
new.
Let's get our creator tag value and have that equal to the player that did the damage.
Right.
And we have that right because we have that right here on shoot that came through with our remote event.
What else do we need?
Let's do the game.
Dot debris service.
Add item creator tag and make it disappear in 2 seconds.
So we have to do something with this in 2 seconds after we tag the person.
They can't just walk around with a tag for like 45 minutes.
Then when they die because they jumped off of something, you get credit.
All right.
Now we're going to do another function.
This is going to be called un tag humanoid.
I'll pass in the humanoid.
I'll just say HUME.
Easier to type.
Now I'm going to do a for loop for I and V in pairs.
HUME Get children.
Do.
We're going to remove all the creator tags.
Every time somebody hits somebody, we're going to take other people's creator tags off so they don't
get credit to just the person who did the kill.
So V is a tag, maybe a tag if there's one on the humanoid when I check though, if a V is a object
of value
and V dot name equals equals o lowercase creator, then v destroy, right?
So get rid of anybody else's so that they don't get credit.
Only you get credit for the kill the last person to touch them.
Then what we're going to do here?
Get this UN tag humanoid.
Copy it.
Right before we do the take damage.
Right.
Check to see if the humanoid exists and tag the humanoid and then tag the humanoid with your tag un.
Tag everybody else's stuff.
Tag your stuff.
Now what?
Well, now what we have to do is get the players died event and start doing stuff with that tagging
system so we can get points and things like that.
Let's go to server script service, open up the game manager.
When a player is added to the game, we add this board that's good in all, but we need to do more than
that.
So I'm going to add this anonymous function, right?
There's no name on it.
It's just called when Player added is fired.
I'm going to add the board pass in the player this time because it's not an automatic connection.
Then I'm going to get the player character added event.
Connect that to another anonymous function.
Character is going to get passed in.
So player added fires when you enter the game, character added fires when your character is added to
the game.
So if you die and you respawn character added is going to fire every single time you're responding,
this only fires when you enter the game.
So what are we going to do?
Let's get our humanoid from the character because now we've got a character, right?
It's Char.
We'll do a wait for child just in case there's a little slow starting up and then we're going to get
our humanoid.
Now, with the humanoid, we can capture the diet event.
We're going to say HUME Dot died.
We're going to connect that to another anonymous function.
Nothing gets passed in there and we'll get, oh, you know what we should do.
I don't know if you noticed this, but you can actually shoot your gun after you die until you respond
as a roadblocks glitch, in my opinion.
But we're going to fix that.
We're going to say.
HUME on equip tools, just in case you're holding anything for that two or 3 seconds until they respond
that way, you can't fire after you're dead.
So we got that taken care of.
What else are we going to do?
We've got to get our tag from the humanoid.
Right.
The whole reason we're here.
Fine.
First child.
Whoops, I always do that.
Lowercase Creator.
So we're going to look for the creator tag on the humanoid.
There should only be one if tag and tag dot value.
So we want to make sure that the tag exists and the tag has the value just in case something weird happened
and we didn't assign the value, say local.
I'm going to call this e player for enemy player.
This is going to be the enemy player's name.
That's the tag value.
So we died.
Someone else shot us.
That is going to be the one who shot us.
We're going to give them credit.
We're going to say e player leader stats dot kills, make sure kills is on your leader stats value plus
equals one.
Take the old value out of one and then let's give them some points to E Player leader stats.
I spell that right.
Leader Stats points.
How many points do we give them?
Let's give them 50.
50 points.
Now, how do you keep track of deaths?
Well, we don't need to be tagged for the death.
We just need to count it.
So underneath this if statement for tagging player.
So not the enemy player but the player who died.
We'll say leader stats.
Dot deaths.
Value plus equals one.
Right.
Make sure these are spelled the same as that.
It's on your leaderboard.
The name of of the metric on your leaderboard.
And we should be good to go.
All right.
We need to test this out and the test server because we need more than one player.
Let's go to where is it view test.
There it is.
And two players.
That's good.
I'm going to hit the start button.
Then I'm going to pause the video.
All this starts up because it takes a long time.
All right.
Our game is starting up.
I'm going to get my pistol.
There's my other guy.
Oh, he's got his invulnerability bubble on.
Hold on.
Cool.
And if you'll notice, if I die, I'm going to go here.
I'm going to reset my character, and now I'm clicking, but I'm not shooting.
Right?
So we fix that problem.
Before we had that problem, you could you could still shoot while you are dying if you're holding the
weapon in your hand.